﻿using System;
using System.IO;
using System.Windows;
using System.Windows.Media;
using System.Windows.Media.Imaging;

namespace MvvmFx.Common.Helpers
{
    /// <summary>
    /// Utility class for image functions
    /// </summary>
    public static class ImageHelper
    {
        /// <summary>
        /// Opens an Image file as an ImageSource
        /// </summary>
        /// <param name="fileName"></param>
        /// <returns></returns>
        public static ImageSource GetImageSource(string fileName)
        {
            if (!File.Exists(fileName))
                throw new FileNotFoundException("Image file not found", fileName);

            Uri uri = new Uri(fileName, UriKind.Absolute);
            BitmapImage bmpImage = new BitmapImage(uri);
            return bmpImage;
        }

        /// <summary>
        /// Create an image source from a system.drawing.bitmap
        /// </summary>
        /// <param name="bitmap"></param>
        /// <returns></returns>
        public static ImageSource GetImageSource(System.Drawing.Bitmap bitmap)
        {
            return System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(
                bitmap.GetHbitmap(), 
                IntPtr.Zero, Int32Rect.Empty,
                BitmapSizeOptions.FromEmptyOptions());
        }

    }
}